package edu.bluebeanforum.service.impl;

import edu.bluebeanforum.common.AppResult;
import edu.bluebeanforum.common.ResultCode;
import edu.bluebeanforum.exception.ApplicationException;
import edu.bluebeanforum.mapper.BoardMapper;
import edu.bluebeanforum.model.Board;
import edu.bluebeanforum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.image.RescaleOp;
import java.util.List;
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALID.toString());
            throw  new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALID));

        }
//        调用mapper
        return boardMapper.selectByNum(num);
    }

    @Override
    public Board selectById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString()+"id"+id);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString()+"board id"+id);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));

        }
//        查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString(),"board="+board);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
//        更新帖子数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        boardMapper.updateByPrimaryKeySelective(updateBoard);
    }
    //删除当前用户的帖子数  id是板块id
    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString()+"board id"+id);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));

        }
//        查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString(),"board="+board);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }

//      更新帖子数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if (updateBoard.getArticleCount()<0) {
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row!=1) {
            log.warn(ResultCode.FAILED.toString(),"受影响的行数不等于1");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }
}
